﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Garden.Model;
using System.IO;
using RegexHelper = Garden.Helper.RegexHelper;
namespace Garden.Func
{
    class Utility
    {
        #region constant
        //种子库文件名
        public static readonly string SEEDLISTFILENAME = "seedlist.xml";
        //谷物库文件名
        public static readonly string GRANARYFILENAME = "granary.xml";
        //农夫配置文件名
        public static readonly  string GARDENCONFIGFILENAME = "GardenConfig.xml";
        //农夫配置文件模板
        public static readonly  string GARDENCONFIGFILETEMPLET =
            "<?xml version=\"1.0\" encoding=\"utf-8\" ?><config><steal></steal><autoSow>0</autoSow><sowid>-1</sowid><water>1</water><vermin>1</vermin><grass>1</grass></config>";
        #endregion


        public static Farmland CreateFarmLand(GroupCollection group)
        {
            Farmland land = new Farmland
            {
                Water =
                    int.Parse(RegexHelper.GetFirstVal(group[0].Value, RegexHelper.WaterReg)),
                FarmNum =
                    int.Parse(RegexHelper.GetFirstVal(group[0].Value, RegexHelper.FarmNumReg)),
                Status =
                    int.Parse(RegexHelper.GetFirstVal(group[0].Value, RegexHelper.StatusReg)),
                FruitNum =
                    int.Parse(RegexHelper.GetFirstVal(group[0].Value, RegexHelper.FarmNumReg)),
                IsLoveFarm =
                    int.Parse(RegexHelper.GetFirstVal(group[0].Value, RegexHelper.SharedReg)) !=
                    0,
                IsMyLoveFarm =
                    int.Parse(RegexHelper.GetFirstVal(group[0].Value, RegexHelper.SharedReg)) ==
                    2,
                IsGrass =
                    int.Parse(RegexHelper.GetFirstVal(group[0].Value, RegexHelper.GrassReg)) ==
                1,
                IsVermin =
                    int.Parse(RegexHelper.GetFirstVal(group[0].Value, RegexHelper.VerminReg)) ==
                1,
                Cropsstatus = RegexHelper.GetFirstVal(group[0].Value, RegexHelper.CropsstatusReg)

            };
            if (RegexHelper.GetFirstVal(group[0].Value, RegexHelper.GrowReg) == string.Empty)
            {
                land.Growpercent = 0;
            }
            else
            {
                land.Growpercent = Math.Round(int.Parse(RegexHelper.GetFirstVal(group[0].Value, RegexHelper.GrowReg)) /
                                                float.Parse(RegexHelper.GetFirstVal(group[0].Value, RegexHelper.TotalgrowReg)), 2)
                 ;
            }
            if (RegexHelper.GetFirstVal(group[0].Value, RegexHelper.SeedidReg) == string.Empty)
            {
                land.Seed = new Seed
                {
                    ID = string.Empty,
                    name = "没有种植"
                };
            }
            else
            {
                land.Seed = new Seed
                {
                    ID = RegexHelper.GetFirstVal(group[0].Value, RegexHelper.SeedidReg),
                    name = RegexHelper.GetFirstVal(group[0].Value, RegexHelper.NameReg)
                };
            }
            land.Crops = FarmlandState.Normal;
            if (RegexHelper.GetFirstVal(group[0].Value, RegexHelper.CropsReg) != string.Empty)
            {
                string val = RegexHelper.GetFirstVal(group[0].Value, RegexHelper.CropsReg);
                if (val.Contains("已偷"))
                {
                    land.Crops = FarmlandState.Stealed;
                }
                else if (val.Contains("分可偷"))
                {
                    land.Crops = FarmlandState.Protect;
                }
                else if (val.Contains("枯死"))
                {
                    land.Crops = FarmlandState.Lose;
                }
            }
            return land;
        }
        /// <summary>
        /// 是否存在种子库文件
        /// </summary>
        /// <returns></returns>
        public static bool IsExistSeedListXMLFile()
        {
            FileInfo file = new FileInfo(SEEDLISTFILENAME);
            return file.Exists;
        }
        /// <summary>
        /// 是否存在谷物库文件
        /// </summary>
        /// <returns></returns>
        public static bool IsExistGranaryXMLFile()
        {
            FileInfo file = new FileInfo(GRANARYFILENAME);
            return file.Exists;
        }

        public static void CreateOrUpdateXMLFile(string fileName,string data)
        {
            FileInfo file = new FileInfo(fileName);
            StreamWriter sw = new StreamWriter(file.Create());
            sw.Write(data);
            sw.Close();
        }
    }
}
